import React from 'react';
import {connect} from 'dva'
import {Row, Col, Form, Select, message} from 'antd';
import Constants from '../../utils/Constants';
const R = require('ramda');
const FormItem = Form.Item;

export default connect(({logisticsSupplier, customerManage, customs }) => ({logisticsSupplier, customerManage, customs }))(Form.create({wrappedComponentRef: true})(class AccountInfos extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            open: false,
            disabled: true,
            type: "",

        }
    }


    handleResetFields = () => {
        this.props.form.resetFields()
        this.props.dispatch({type: 'customerManage/setRecAcc', payload: {recAcc: {}}});
        this.props.dispatch({type: 'customs/refreshState', payload: {list: []} });
    }

    handleOpen = (type) => {
        this.state.type = type;
        this.setState({open: true})
    }

    setStatementType = (value) => {
        let stObj = this.props.form.getFieldValue("stObj");
        this.props.setStatementType(stObj, value);
    }

    handleConfirm = () => {
        let flag = false;
        this.props.form.validateFields((errors, values) => {
            if (errors) return;

            flag = true;
            this.props.setStatementType(values.stObj, values.stObjId);
        })

        return flag;
    }

        getProvider = (e) => {
            this.props.form.setFieldsValue({stObjId: ''});
            let values = this.props.form.getFieldsValue();
            let type = R.isNil(e) ? values.stObj : e;
            if (type === '0') {
                this.props.dispatch({type: 'customerManage/queryCustomerManageList', payload: {status: 4}});//查询工厂供应商列表
            } else if(type === '1'){
                this.props.dispatch({type: 'logisticsSupplier/getSupplierList'});
            }else {
                this.props.dispatch({ type: 'customs/getListForPage', payload: { page: Constants.DEFAULT_PAGE } }); //查询海关信息
            }
        };


    render() {
        const {getFieldDecorator} = this.props.form;
        const formItemLayout = {labelCol: {xs: {span: 8}, sm: {span: 5}}, wrapperCol: {xs: {span: 15}, sm: {span: 14}}};
        let suppilerList = [];
        let gcsuppilerList = R.isNil(this.props.customerManage.suppilerList) ? [] : this.props.customerManage.suppilerList;
        let wlsuppilerList = R.isNil(this.props.logisticsSupplier.supplierList) ? [] : this.props.logisticsSupplier.supplierList;
        let hgsuppilerList = R.isNil(this.props.customs.list) ? [] : this.props.customs.list;

        let stObjType = this.props.form.getFieldValue('stObj');
        suppilerList = stObjType === '0' ? gcsuppilerList : (stObjType === "1" ? wlsuppilerList : hgsuppilerList);


        return (

            <Form>
                <FormItem {...formItemLayout} label="结算对象类型" hasFeedback>
                    {getFieldDecorator('stObj', {
                        rules: [{
                            required: true, message: '请选择结算对象类型'
                        }],
                        initialValue: ''
                    })(
                        <Select style={{width: '100%'}} disabled={this.props.disabled}
                                onChange={value => this.getProvider(value)}>
                            <Select.Option value={"0"}>商品供应商结算</Select.Option>
                            <Select.Option value={"1"}>物流供应商结算</Select.Option>
                            <Select.Option value={"2"}>海关</Select.Option>
                        </Select>
                    )}
                </FormItem>

                <FormItem {...formItemLayout} label="结算对象" hasFeedback>
                    {getFieldDecorator('stObjId', {
                        rules: [{
                            required: true, message: '请选择结算对象'
                        }],
                        initialValue: ''
                    })(
                        <Select style={{width: '100%'}} disabled={this.props.disabled}
                                onChange={this.setStatementType.bind(this)}
                                showSearch allowClear
                                placeholder="可通过输入关键字搜索"
                                filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}>
                            {stObjType != 0 ?
                                (!R.isNil(suppilerList) ? suppilerList.map(item => {
                                    return (<Select.Option value={item.id + ""}
                                                           key={item.id + ""}>{item.companyname}</Select.Option>);
                                }) : '')

                                :

                                (!R.isNil(suppilerList) ? suppilerList.map(item => {
                                    return (<Select.Option value={item.id + ""}
                                                           key={item.id + ""}>{item.companynameen}({item.companyname})</Select.Option>);
                                }) : '')
                            }
                        </Select>
                    )}
                </FormItem>
            </Form>
    );
    }
    })
    )

    // WEBPACK FOOTER //
    // ./public/src/components/order/components/AccountInfos.js